home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSFINFO.C < prev    next >
C/C++ Source or Header  |  1995-05-24  |  8KB  |  295 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsfinfo.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains to query and set file information.
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <bs.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Close find first/next operation and free data
  49. //    Parameters:    pv         Find first/next data structure
  50. //       Returns:    TRUE if successful.
  51. //----------------------------------------------------------------------------
  52. BOOL FN_E FinfoFindClose(PVOID pv)
  53. {
  54.     if (pv)                                        // Free data structure
  55.         MemFree(pv);
  56.     return TRUE;
  57. }
  58.  
  59. //----------------------------------------------------------------------------
  60. //   Description:    Convert between find first/next and file info structure
  61. //    Parameters:    pv         Find first/next data structure
  62. //                        pfinfo    File info structure
  63. //       Returns:    
  64. //----------------------------------------------------------------------------
  65. #if OS_UNIX==0
  66. static VOID FN_L FinfoFindConvert(PVOID pv, PBS_FINFO pfinfo)
  67. {
  68.     struct find_t *fffn = (struct find_t *)pv;
  69.     struct tm tm_file;
  70.     if (!pfinfo)
  71.         return ;
  72.  
  73.     memset(pfinfo, 0, sizeof(BS_FINFO));
  74.     strcpy(pfinfo->szFname, fffn->name);
  75.     pfinfo->usAttrib = (USHORT)fffn->attrib;
  76.     pfinfo->lSize      = (LONG)fffn->size;
  77.  
  78.     memset(&tm_file, 0, sizeof(tm_file));
  79.     tm_file.tm_sec      = (fffn->wr_time & 0x001F) * 2;
  80.     tm_file.tm_min      = ((fffn->wr_time >> 5) & 0x003F);
  81.     tm_file.tm_hour  = ((fffn->wr_time >> 11) & 0x001F);
  82.     tm_file.tm_mday  = (fffn->wr_date & 0x001F);
  83.     tm_file.tm_mon      = ((fffn->wr_date >> 5) & 0x000F) - 1;
  84.     tm_file.tm_year  = ((fffn->wr_date >> 9) & 0x007F) + 80;
  85.     pfinfo->timet = mktime(&tm_file);
  86.     return ;
  87. }
  88. #endif
  89.  
  90.  
  91. //----------------------------------------------------------------------------
  92. //   Description:    Find first file.
  93. //    Parameters:    pcsz         File specification
  94. //                        usAttrib    File attribute mask
  95. //                        pfinfo    File information structure
  96. //       Returns:    Pointer to find first/next data structure or NULL.
  97. //----------------------------------------------------------------------------
  98. PVOID FN_E FinfoFindFirst(PCSZ pcsz, USHORT usAttrib, PBS_FINFO pfinfo)
  99. {
  100. #if OS_UNIX
  101.     NOTUSED(pcsz);
  102.     NOTUSED(usAttrib);
  103.     NOTUSED(pfinfo);
  104.     return FALSE;
  105. #else
  106.     PVOID pv = MemAlloc(sizeof(struct find_t));
  107.     CHAR szPath[MAX_PATH];
  108.  
  109.     if (pv == NULL)
  110.         return pv;
  111.  
  112.     Assert(pcsz);
  113.     FnameNormalize(strcpy(szPath, pcsz), FNAME_DIR);
  114.     if (_dos_findfirst(szPath, (int)usAttrib, pv))
  115.         {
  116.         MemFree(pv);
  117.         return NULL;
  118.         }
  119.     if (pfinfo)
  120.         FinfoFindConvert(pv, pfinfo);
  121.     return pv;
  122. #endif
  123. }
  124.  
  125.  
  126. //----------------------------------------------------------------------------
  127. //   Description:    Find next file    
  128. //    Parameters:    pv            Find first/next structure
  129. //                        pfinfo    File information structure
  130. //       Returns:    TRUE if file found.
  131. //----------------------------------------------------------------------------
  132. BOOL FN_E FinfoFindNext(PVOID pv, PBS_FINFO pfinfo)
  133. {
  134. #if OS_UNIX
  135.     NOTUSED(pv);
  136.     NOTUSED(pfinfo);
  137.     return FALSE;
  138. #else
  139.     Assert(pv);
  140.     // finds 2 files, pf.db and cs.db..neither one works.
  141.     if (_dos_findnext(pv))
  142.         return FALSE;
  143.     if (pfinfo)
  144.         FinfoFindConvert(pv, pfinfo);
  145.     return TRUE;
  146. #endif
  147. }
  148.  
  149.  
  150. //----------------------------------------------------------------------------
  151. //   Description:    Get file information
  152. //    Parameters:    pcsz        File name
  153. //                        pfinfo    File information
  154. //       Returns:    TRUE if successful.
  155. //----------------------------------------------------------------------------
  156. BOOL FN_E FinfoGet(PCSZ pcsz, PBS_FINFO pfinfo)
  157. {
  158. #if OS_UNIX
  159.     NOTUSED(pcsz);
  160.     Assert(pcsz && pfinfo);
  161.     memset(pfinfo, 0, sizeof(BS_FINFO));
  162.     return TRUE;
  163. #else
  164.     struct find_t fffn;
  165.     CHAR szPath[MAX_PATH];
  166.  
  167.     Assert(pcsz && pfinfo);
  168.     FnameFullPath(strcpy(szPath, pcsz));
  169.     if (_dos_findfirst(szPath, FA_ARCHIVE|FA_READONLY|FA_SYSTEM|FA_HIDDEN, &fffn))
  170.         return Error("File not found.\n'%s'", szPath);
  171.  
  172.     FinfoFindConvert((PVOID)&fffn, pfinfo);
  173.     return TRUE;
  174. #endif
  175.  
  176. }
  177.  
  178.  
  179. //----------------------------------------------------------------------------
  180. //   Description:    Set file information
  181. //    Parameters:    pcsz        File name
  182. //                        pfinfo    File information
  183. //       Returns:    TRUE if successful.
  184. //----------------------------------------------------------------------------
  185. BOOL FN_E FinfoSet(PCSZ pcsz, PBS_FINFO pfinfo)
  186. {
  187. #if OS_UNIX
  188.     NOTUSED(pcsz);
  189.     NOTUSED(pfinfo);
  190.     return TRUE;
  191. #else
  192.     USHORT usAttrib;
  193.     USHORT usDate;
  194.     USHORT usTime;
  195.     struct tm *tm;
  196.     HF hf;
  197.     CHAR szPath[MAX_PATH];
  198.  
  199.     Assert(pcsz && pfinfo);
  200.     FnameFullPath(strcpy(szPath, pcsz));
  201.     tm = localtime(&pfinfo->timet);
  202.     usDate = (USHORT)((tm->tm_mday & 0x001F)
  203.         + (((tm->tm_mon + 1) & 0x000F) << 5)
  204.         + (((tm->tm_year - 80) & 0x007F) << 9));
  205.     usTime = (USHORT)(((tm->tm_sec / 2) & 0x001F)
  206.         + ((tm->tm_min & 0x003F) << 5)
  207.         + ((tm->tm_hour & 0x001F) << 11));
  208.  
  209.  
  210.     if (_dos_getfileattr(szPath, (unsigned *)&usAttrib))
  211.         return Error("Could not get file attributes.");
  212.  
  213.     usAttrib &= ~(_A_RDONLY|_A_HIDDEN|_A_SYSTEM);
  214.     if (_dos_setfileattr(szPath, usAttrib))
  215.         return Error("Could not set file attributes.");
  216.  
  217.     if (!FileOpen(&hf, szPath, FL_OPEN|FL_READWRITE|FL_DENYWRITE|FL_BINARY, NULL))
  218.         return FALSE;
  219.     if (_dos_setftime(hf, usDate, usTime))
  220.         {
  221.         FileClose(hf);
  222.         return Error("Could not set file attributes.");
  223.         }
  224.     if (!FileClose(hf))
  225.         return FALSE;
  226.  
  227.     if (_dos_setfileattr(szPath, pfinfo->usAttrib))
  228.         return Error("Could not set file attributes.");
  229.  
  230.     return TRUE;
  231. #endif
  232. }
  233.  
  234.  
  235. //----------------------------------------------------------------------------
  236. //   Description:    Run standard test suite
  237. //    Parameters:    sTest        Test to run.
  238. //                                        0        Run all default tests (except).
  239. //       Returns:    TRUE if successful.
  240. //----------------------------------------------------------------------------
  241. #if COMPILE_TEST
  242. BOOL FN FinfoTest(SHORT sTest)
  243. {
  244.     BS_FINFO finfo;
  245.     PVOID pv;
  246.     struct tm *tm;
  247.  
  248.     NOTUSED(sTest);
  249.     pv = FinfoFindFirst("*.*", 0, &finfo);
  250.     if (pv)
  251.         {
  252.         do
  253.             {
  254.             tm = localtime(&finfo.timet);
  255.             Output("%-16.16s  %9ld  %04X  %2d:%02d:%02d %2d/%02d/%4d\n",
  256.                  finfo.szFname,
  257.                 finfo.lSize,
  258.                 finfo.usAttrib,
  259.                 tm->tm_hour, tm->tm_min, tm->tm_sec,
  260.                 tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
  261.             }
  262.         while (FinfoFindNext(pv, &finfo));
  263.         FinfoFindClose(pv);
  264.         }
  265.     if (!FinfoGet("bsfinfo.c", &finfo))
  266.         return FALSE;
  267.     tm = localtime(&finfo.timet);
  268.     Output("%-16.16s  %9ld  %04X  %2d:%02d:%02d %2d/%02d/%4d\n",
  269.         finfo.szFname,
  270.         finfo.lSize,
  271.         finfo.usAttrib,
  272.         tm->tm_hour, tm->tm_min, tm->tm_sec,
  273.         tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
  274.  
  275.     finfo.usAttrib &= ~FA_ARCHIVE;
  276.     finfo.timet--;
  277.     if (!FinfoSet("bsfinfo.c", &finfo))
  278.         return FALSE;
  279.  
  280.     if (!FinfoGet("bsfinfo.c", &finfo))
  281.         return FALSE;
  282.     tm = localtime(&finfo.timet);
  283.     Output("%-16.16s  %9ld  %04X  %2d:%02d:%02d %2d/%02d/%4d\n",
  284.         finfo.szFname,
  285.         finfo.lSize,
  286.         finfo.usAttrib,
  287.         tm->tm_hour, tm->tm_min, tm->tm_sec,
  288.         tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900);
  289.     return TRUE;
  290. }
  291. #endif
  292. //----------------------------------------------------------------------------
  293. //------------------------------- End of File --------------------------------
  294. //----------------------------------------------------------------------------
  295.